package com.xiaodu.dynamicdatasource.config;

import org.springframework.lang.NonNull;

import java.util.HashSet;
import java.util.Set;

/**
 * @author: dutianjie
 * @date: 2021/4/22
 * @version: v1.0.0
 * @description: 描述
 **/
public class DataSourceContextHolder {

    private static final ThreadLocal<String> DATASOURCE_THREADLOCAL = new ThreadLocal<>();

    public static final Set<String> datasourceKeySet = new HashSet<>();

    public static String getDataSourceKey() {
        return DATASOURCE_THREADLOCAL.get();
    }

    public static void setDataSourceKey(String key) {
        DATASOURCE_THREADLOCAL.set(key);
    }

    public static void removeDataSourceKey() {
        DATASOURCE_THREADLOCAL.remove();
    }

    public static boolean validateDataSourceKey(@NonNull String key) {
        return datasourceKeySet.stream().anyMatch(key::equalsIgnoreCase);
    }

}
